﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common.Logging;
using WuDada.Core.SystemApplications;
using WuDada.Core.SystemApplications.Service;
using WuDada.Core.SystemApplications.Domain;
using WuDada.Core.Generic.Mail;
using WuDada.Core.Post.Domain;
using System.Text;
using WuDada.Core.Member.Domain;
using WuDada.Core.Auth;
using WuDada.Core.Auth.Service;
using WuDada.Core.Auth.Domain;

/// <summary>
/// WebMailService 的摘要描述
/// </summary>
public class WebMailService
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private MailService m_MailService;
    private SystemFactory m_SystemFactory;
    private ISystemService m_SystemService;
    private AuthFactory m_AuthFactory;
    private IAuthService m_AuthService;
    private SystemParamVO m_MailVO;

    public WebMailService()
    {
        m_SystemFactory = new SystemFactory();
        m_SystemService = m_SystemFactory.GetSystemService();
        m_AuthFactory = new AuthFactory();
        m_AuthService = m_AuthFactory.GetAuthService();

        m_MailVO = m_SystemService.GetSystemParamByRoot();

        bool enableSSL = m_MailVO.EnableSSL;
        int port = 25;

        if (m_MailVO.MailSmtp.IndexOf("gmail") != -1)
        {
            enableSSL = true;
            port = 587;
        }
        else if (!string.IsNullOrEmpty(m_MailVO.MailPort))
        {
            port = int.Parse(m_MailVO.MailPort);
        }

        m_MailService = new MailService(m_MailVO.MailSmtp, port, enableSSL, m_MailVO.Account, m_MailVO.Password);
    }

    /// <summary>
    /// 發信給設定的信箱 By 訊息
    /// </summary>
    /// <param name="messageVO"></param>
    public void SendMail_ToContactor_ByMessage(MessageVO messageVO)
    {
        try
        {
            string classify = "線上諮詢收件者";
            IList<ItemParamVO> contactorList = m_SystemService.GetAllItemParamByNoDel(classify);

            //系統管理員
            IList<LoginUserVO> adminUser = m_AuthService.GetAllLoginUserList();

            StringBuilder sbMailList = new StringBuilder();
            //設定的信箱
            if (contactorList != null && contactorList.Count > 0)
            {
                foreach (ItemParamVO contactor in contactorList)
                {
                    sbMailList.Append(string.Format("{0};", contactor.Value));
                }
            }
            //系統管理員的信箱
            if (adminUser != null && adminUser.Count > 0)
            {
                foreach (LoginUserVO loginUserVO in adminUser)
                {
                    if (loginUserVO.IsAlive == 1 && !string.IsNullOrEmpty(loginUserVO.Email))
                    {
                        sbMailList.Append(string.Format("{0};", loginUserVO.Email));
                    }
                }
            }
            m_Log.Debug("sbMailList = " + sbMailList.ToString());
            if (!string.IsNullOrEmpty(sbMailList.ToString()))
            {
                SystemParamVO mailVO = m_SystemService.GetSystemParamByRoot();
                MailService mailService = new MailService(mailVO.MailSmtp, int.Parse(mailVO.MailPort), mailVO.EnableSSL, mailVO.Account, mailVO.Password);                

                string mailTitle = string.Format("收到一封由【{0}】從富裕自由國際管顧問股份有限公司網站提出的詢問信。", messageVO.CreateName);
                string mailContent = GenMailContent(messageVO);

                mailService.SendMail(mailVO.SendEmail, sbMailList.ToString(), mailTitle, mailContent);
            }
        }
        catch (Exception ex)
        {
            m_Log.Error(ex);
        }
    }

    private string GenMailContent(MessageVO messageVO)
    {
        StringBuilder sbContent = new StringBuilder();

        sbContent.Append(string.Format("時　　間：{0}<br />", messageVO.CreatedDate.Value.ToString()));
        sbContent.Append(string.Format("姓　　名：{0}<br />", messageVO.CreateName));
        sbContent.Append(string.Format("手　　機：{0}<br />", messageVO.Mobile));
        sbContent.Append(string.Format("電子信箱：{0}<br />", messageVO.EMail));
        sbContent.Append(string.Format("主　　旨：{0}<br />", messageVO.Title));
        sbContent.Append(string.Format("問　　題：<br />{0}<br />", messageVO.Content.Replace("\n", "<br />")));

        return sbContent.ToString();
    }

    /// <summary>
    /// 發信給設定的信箱 By 訊息
    /// </summary>
    /// <param name="messageVO"></param>
    public void SendMail_ToMember_ByForgetPassword(MemberVO memberVO)
    {
        try
        {
            if (memberVO != null)
            {
                SystemParamVO mailVO = m_SystemService.GetSystemParamByRoot();
                MailService mailService = new MailService(mailVO.MailSmtp, int.Parse(mailVO.MailPort), mailVO.EnableSSL, mailVO.Account, mailVO.Password);

                StringBuilder sbMailList = new StringBuilder();
                sbMailList.Append(string.Format("{0};", memberVO.Email));

                string mailTitle = "【富裕自由國際管顧問股份有限公司】您的會員密碼。";
                string mailContent = GenMailContent_ForgetPassword(memberVO);

                mailService.SendMail(mailVO.SendEmail, sbMailList.ToString(), mailTitle, mailContent);
            }
        }
        catch (Exception ex)
        {
            m_Log.Error(ex);
        }
    }

    private string GenMailContent_ForgetPassword(MemberVO memberVO)
    {
        StringBuilder sbContent = new StringBuilder();

        sbContent.Append(string.Format("會員密碼：{0}<br />", memberVO.LoginPassword));

        return sbContent.ToString();
    }
}